package com.dqhl.business.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.dqhl.business.Base.BaseActivity;
import com.dqhl.business.R;
import com.dqhl.business.modle.User;
import com.dqhl.business.util.Config;
import com.dqhl.business.util.Dlog;
import com.dqhl.business.util.FormatUtils;
import com.dqhl.business.util.JsonUtils;
import com.dqhl.business.util.UserSaveUtil;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * description: 登录页
 * autour: guanluocang
 * date: 2016/11/3 14:02
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView iv_register;
    private ImageView iv_login;
    private EditText et_telPhone;
    private EditText et_password;
    private Button btn_pwd_show;
    private boolean isHidden = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        context = this;
        initView();
        isLogin();
    }

    private void isLogin() {
        if (user != null) {
            Dlog.e("user:" + user.toString());
            forward(MainActivity.class);
        }
    }

    private void initView() {

        (iv_login) = (ImageView) findViewById(R.id.iv_login);
        (iv_register) = (ImageView) findViewById(R.id.iv_register);
        iv_login.setOnClickListener(this);
        iv_register.setOnClickListener(this);
        (et_password) = (EditText) findViewById(R.id.et_password);
        (et_telPhone) = (EditText) findViewById(R.id.et_telPhone);
        (btn_pwd_show) = (Button) findViewById(R.id.btn_pwd_show);
        btn_pwd_show.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pwd_show:
                if (isHidden) {
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    btn_pwd_show.setBackgroundResource(R.drawable.ic_registeropen);
                } else {
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    btn_pwd_show.setBackgroundResource(R.drawable.ic_registerclose);
                }
                isHidden = !isHidden;
                et_password.postInvalidate();
                //切换后将EditText光标置于末尾
                CharSequence charSequence = et_password.getText();
                if (charSequence instanceof Spannable) {
                    Spannable spanText = (Spannable) charSequence;
                    Selection.setSelection(spanText, charSequence.length());
                }
                break;
            case R.id.iv_login:
                doLogin();
                break;
            case R.id.iv_register:
                overlay(RegisterActivity.class);
                break;
        }
    }

    private void doLogin() {
        String telPhone = et_telPhone.getText().toString().trim();
        String password = et_password.getText().toString().trim();
        if (!FormatUtils.isMobileNO(telPhone)) {
            toast("手机号格式不正确");
        } else if (!FormatUtils.isPwdLegal(password)) {
            toast("密码为6~12数字字母");
        } else {
            showCustomLoadBar(".");
            RequestParams params = new RequestParams(Config.login);
            params.addBodyParameter("mobile_phone", telPhone);
            params.addBodyParameter("password", password);
            x.http().post(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    Dlog.e(result);
                    int errCode = JsonUtils.getErrCode(result);
                    String errMsg = JsonUtils.getErrMsg(result);
                    String data = JsonUtils.getData(result);
                    if (0 == errCode) {
                        User u = JSON.parseObject(data, User.class);
                        UserSaveUtil.saveUser(context, u);
                        Dlog.e(u.toString());
                        forward(MainActivity.class);
                    }
                    toast(errMsg);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {
                    hideCustomLoadBar();
                }
            });
        }
    }
}
